/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.cib.fintech.entrustedinvestment.biz.service.impl;

import com.cib.fintech.entrustedinvestment.biz.domain.TransferOrder;
import com.cib.fintech.entrustedinvestment.utils.ValidationUtil;
import com.cib.fintech.entrustedinvestment.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.cib.fintech.entrustedinvestment.biz.repository.TransferOrderRepository;
import com.cib.fintech.entrustedinvestment.biz.service.TransferOrderService;
import com.cib.fintech.entrustedinvestment.biz.service.dto.TransferOrderDto;
import com.cib.fintech.entrustedinvestment.biz.service.dto.TransferOrderQueryCriteria;
import com.cib.fintech.entrustedinvestment.biz.service.mapstruct.TransferOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.cib.fintech.entrustedinvestment.utils.PageUtil;
import com.cib.fintech.entrustedinvestment.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhangqingkun
* @date 2020-09-18
**/
@Service
@RequiredArgsConstructor
public class TransferOrderServiceImpl implements TransferOrderService {

    private final TransferOrderRepository transferOrderRepository;
    private final TransferOrderMapper transferOrderMapper;

    @Override
    public Map<String,Object> queryAll(TransferOrderQueryCriteria criteria, Pageable pageable){
        Page<TransferOrder> page = transferOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(transferOrderMapper::toDto));
    }

    @Override
    public List<TransferOrderDto> queryAll(TransferOrderQueryCriteria criteria){
        return transferOrderMapper.toDto(transferOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TransferOrderDto findById(Long id) {
        TransferOrder transferOrder = transferOrderRepository.findById(id).orElseGet(TransferOrder::new);
        ValidationUtil.isNull(transferOrder.getId(),"TransferOrder","id",id);
        return transferOrderMapper.toDto(transferOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TransferOrderDto create(TransferOrder resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return transferOrderMapper.toDto(transferOrderRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TransferOrder resources) {
        TransferOrder transferOrder = transferOrderRepository.findById(resources.getId()).orElseGet(TransferOrder::new);
        ValidationUtil.isNull( transferOrder.getId(),"TransferOrder","id",resources.getId());
        transferOrder.copy(resources);
        transferOrderRepository.save(transferOrder);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            transferOrderRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TransferOrderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TransferOrderDto transferOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("客户产品代码", transferOrder.getCustPrdCode());
            map.put("托管行产品代码", transferOrder.getProductCode());
            map.put("客户产品名称", transferOrder.getCustPrdName());
            map.put("管理人机构号", transferOrder.getManageId());
            map.put("管理人userid", transferOrder.getManageeUserId());
            map.put("金额", transferOrder.getAmount());
            map.put("收付方向", transferOrder.getDirection());
            map.put("付款账号", transferOrder.getCreditAccount());
            map.put("付款账号名称", transferOrder.getCreditAccountName());
            map.put("付款账号开户行号", transferOrder.getBankId());
            map.put("付款账号开户行名", transferOrder.getBankName());
            map.put("收款账号", transferOrder.getDebitAccount());
            map.put("收款账号名称", transferOrder.getDebitAccountName());
            map.put("收款账号开户行号", transferOrder.getFbankId());
            map.put("收款账号开户行名", transferOrder.getFbankName());
            map.put("指令支付日期", transferOrder.getPaymentDate());
            map.put("划款事由", transferOrder.getPaymentReason());
            map.put("附言", transferOrder.getFuyan());
            map.put("划款类型", transferOrder.getPaymentTypeId());
            map.put("流水号", transferOrder.getCustInstructionNo());
            map.put("行内外标识 可选值：1-行内支付；2-跨行支付", transferOrder.getInterBank());
            map.put("证券代码", transferOrder.getZqdm());
            map.put("付款账户币种", transferOrder.getCreditCurrency());
            map.put("收款账户币种", transferOrder.getDebitCurrency());
            map.put("优先级 0-普通 1-高 2-紧急 3-最高", transferOrder.getPriority());
            map.put("理财产品代码", transferOrder.getFinanceProductId());
            map.put("理财产品代码名称", transferOrder.getFinanceProductName());
            map.put("备注1", transferOrder.getBz1());
            map.put("备注2", transferOrder.getBz2());
            map.put("备注3", transferOrder.getBz3());
            map.put("备注4", transferOrder.getBz4());
            map.put("备注5", transferOrder.getBz5());
            map.put("备注6", transferOrder.getBz6());
            map.put("指令模式 0未上线 1已上线", transferOrder.getZltype());
            map.put("渠道 2-电子", transferOrder.getSource());
            map.put("生成方式 SZT/TGT/CZXT/kfpt", transferOrder.getGenerate());
            map.put("租户id", transferOrder.getTenId());
            map.put(" instructionNo",  transferOrder.getInstructionNo());
            map.put(" parentInstructionNo",  transferOrder.getParentInstructionNo());
            map.put(" status",  transferOrder.getStatus());
            map.put(" errorMessage",  transferOrder.getErrorMessage());
            map.put(" createTime",  transferOrder.getCreateTime());
            map.put(" updateTime",  transferOrder.getUpdateTime());
            map.put("同城标志 可选值：0-否；1-是", transferOrder.getSameCity());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}